home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1994
/
MacHack 1994.toast
/
MacHack™94
/
Talks & Papers
/
Michael D. Crawford↵
/
Word Services SDK 1.0.5
/
Writeswell Jr. Source
/
TestBed.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-17
|
14KB
|
713 lines
/* TestBed.c
* Word Services Testbed - main module for Writeswell Jr.
*
* ©1992 Working Software, Inc.
* This source code is copyrighted. Permission is granted to use the Word Services
* portion of the Writeswell Jr. source code in your own programs, but you
* may not distribute the Writeswell Jr. word-processor code as a
* commercial product. If you modify the code, please do not call it
* Writeswell Jr. (or Writeswell.) This will ensure that people understand the
* program and don’t have to deal with a number of different versions with
* who-knows-what going on in the code.
*
* Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
*/
#include <EPPC.h>
#include <AppleEvents.h>
#include <AEObjects.h>
#include <Printing.h>
#include "AERegistry.h"
#include "WordServices.h"
#include "TestBed.h"
#include "TBConstants.h"
#include "MyFiles.h"
#include "GenHandlers.h"
#include "AppEvents.h"
#include "AEObj.h"
#include "InitMenu.h"
#include "Gripe.h"
#include "Scroll.h"
#include "Prefs.h"
#include "DoChecking.h"
#include "ServiceMgr.h"
#include "ObWind.h"
#include "ObText.h"
#include "ObNull.h"
#include "FontMenu.h"
#include "Options.h"
#include "ServiceDialog.h"
#include "PrintWWJr.h"
#include "UnloadStuff.h"
#include "InitWWJr.h"
#include "MyGestalt.h"
#include "PageSetup.h"
#include "MyTextEdit.h"
#define GLOBALS_HERE
#include "TBGlobals.h"
#undef GLOBALS_HERE
Boolean DoMouseDown( EventRecord *eventPtr );
Boolean DoMenuCommand( long menuSpot );
Boolean DoAppleMenu( short theItem );
Boolean DoFileMenu( short theItem );
Boolean DoEditMenu( short theItem );
Boolean DoServMenu( short theItem );
Boolean DoKeyDown( long message, short modifiers );
Boolean DoActivateEvt( short modifiers );
Boolean DoUpdateEvt( long message );
void DoAboutMe( void );
Boolean DoOSEvt( EventRecord *eventPtr );
void GrowDocWindow( WindowPtr theWindow, Point where );
void SetTESize( WindowPtr theWindow );
void MyDrawGrowIcon( WindowPtr theWindow );
void DoCloseWindow( void );
void DoQuit( void );
void DoZoom( WindowPtr theWindow, short deskPart );
void ResetWindowParts( WindowPtr theWindow );
pascal void AboutVersionProc( DialogPtr dlg, short item );
void main( void )
{
short foo;
OSErr err;
MaxApplZone();
InitGraf(&thePort);
InitFonts();
/* FlushEvents(everyEvent, 0); */
InitWindows();
TEInit();
InitDialogs(0L);
gDocDirty = false;
gAppFileRefNum = CurResFile();
gQuitRequested = false;
gPrintRequested = false;
if ( !OpenPrefFile() ){
Gripe( "\pCannot open Writeswell, Jr. Preferences" );
ExitToShell();
}
InitCursor();
if ( System7Installed() ){
err = InitForSystem7();
}else{
err = InitForSystem6();
}
if ( err )
ExitToShell();
while ( EventLoop() )
;
}
Boolean EventLoop( void )
{
EventRecord theEvent;
UnloadStuff();
if ( gPrintRequested ){
gPrintRequested = false;
DoPrint();
}
if ( gQuitRequested ){
DoQuit();
return false; /* We should never reach this statement */
}
WaitNextEvent( everyEvent, &theEvent, 30, (RgnHandle)NULL );
if ( gDocWindow )
TEIdle( (TEHandle)GetWRefCon( gDocWindow ) );
switch( theEvent.what ){
case nullEvent:
DoNullEvent( &theEvent );
break;
case mouseDown:
return DoMouseDown( &theEvent );
break;
case mouseUp:
DoMouseUp();
break;
case autoKey:
case keyDown:
return DoKeyDown( theEvent.message, theEvent.modifiers );
break;
case updateEvt:
DoUpdateEvt( theEvent.message );
break;
case diskEvt:
DoDiskEvt();
break;
case activateEvt:
DoActivateEvt( theEvent.modifiers );
break;
case osEvt:
DoOSEvt( &theEvent );
break;
case kHighLevelEvent:
DoHighLevelEvent( &theEvent );
break;
default:
/*Gripe("\pGot unknown event type" );*/
break;
}
return true;
}
Boolean DoNullEvent( EventRecord *theEventPtr )
{
/* One could execute some sort of background task here */
return true;
}
Boolean DoMouseDown( EventRecord *eventPtr )
{
WindowPtr theWindow;
Boolean result = true;
Rect dragRect;
TEHandle textH;
short partCode;
ControlHandle ctlHdl;
short curFile;
short deskPart;
Boolean shifted;
deskPart = FindWindow( eventPtr->where, &theWindow );
switch( deskPart ){
case inDesk:
break;
case inMenuBar:
FixMenuMarks();
curFile = CurResFile();
UseResFile( gPrefFileRefNum ); /* Make SICN resource accessible */
result = DoMenuCommand( MenuSelect( eventPtr->where ) );
UseResFile( curFile );
FixMenuMarks();
break;
case inSysWindow:
SystemClick( eventPtr, theWindow );
break;
case inContent:
if ( theWindow != FrontWindow() ){
SelectWindow( theWindow );
} else {
GlobalToLocal( &(eventPtr->where) );
partCode = FindControl( eventPtr->where, theWindow, &ctlHdl );
if ( partCode == 0 ){
textH = (TEHandle)GetWRefCon( theWindow );
gScrollWindow = theWindow; /* For use by TrackContentClick */
/* 1.1.1 MDC We use an intermediate variable rather than just passing
* eventPtr->modifiers & shiftKey because this is a two-byte
* value, with a bit set in the high byte. Since a boolean
* is a single byte, this resulted in always passing 0, so we
* didn't see the shift key.
*/
if ( eventPtr->modifiers & shiftKey ){
shifted = true;
}else{
shifted = false;
}
TEClick( eventPtr->where,
shifted,
textH );
}else{
DoControl( theWindow, ctlHdl, partCode, eventPtr->where );
}
}
break;
case inDrag:
DragWindow( theWindow, eventPtr->where, &screenBits.bounds ); /* NOT correct but works */
break;
case inGrow:
GrowDocWindow( theWindow, eventPtr->where );
break;
case inGoAway:
if ( TrackGoAway( theWindow, eventPtr->where ))
DoCloseWindow();
break;
case inZoomIn:
case inZoomOut:
if ( TrackBox( theWindow, eventPtr->where, deskPart ) )
DoZoom( theWindow, deskPart );
break;
}
return result;
}
Boolean DoMenuCommand( long menuSpot )
{
short theMenu;
short theItem;
Boolean result;
theMenu = HiWord( menuSpot );
theItem = LoWord( menuSpot );
switch ( theMenu ){
case 0:
result = true;
break;
case kAppleMenuID:
result = DoAppleMenu( theItem );
break;
case kFileMenuID:
result = DoFileMenu( theItem );
break;
case kEditMenuID:
result = DoEditMenu( theItem );
break;
case kFontMenuID:
result = DoFontMenu( theItem );
break;
case kStyleMenuID:
result = DoStyleMenu( theItem );
break;
case kServMenuID:
result = DoServMenu( theItem );
break;
}
HiliteMenu( 0 );
return result;
}
Boolean DoAppleMenu( short theItem )
{
Handle appleHandle;
Str255 itemName;
switch ( theItem ){
case kAMAboutMe:
DoAboutMe();
break;
case kAMDash:
break;
default:
appleHandle = GetResource( 'MENU', kAppleMenuID );
if ( !appleHandle ){
Gripe( "\pCannot get handle to apple menu" );
return false;
}
GetItem( appleHandle, theItem, itemName );
OpenDeskAcc( itemName );
if ( gDocWindow ){
SetPort( gDocWindow );
}
break;
}
return true;
}
Boolean DoFileMenu( short theItem )
{
switch ( theItem ){
case kFMNew:
if ( gDocWindow )
return true;
return MakeNewWindow() == noErr;
break;
case kFMOpen:
DoOpenDialog();
break;
case kFMClose:
DoCloseWindow();
break;
case kFMSave:
DoSave();
break;
case kFMSaveAs:
DoSaveAs(); /* 1.1.1 MDC */
break;
case kFMPageSetup:
DoPageSetup();
break;
case kFMPrint:
DoPrint();
break;
case kFMQuit:
DoQuit();
break;
}
return true;
}
Boolean DoEditMenu( short theItem )
{
TEHandle textH;
if ( !SystemEdit( theItem - 1 ) ){
if ( gDocWindow ){
textH = (TEHandle)GetWRefCon( gDocWindow );
switch( theItem ){
case kEMCut:
gDocDirty = true;
TECut( textH );
break;
case kEMCopy:
TECopy( textH );
break;
case kEMPaste:
gDocDirty = true;
TEStylPaste( textH );
break;
case kEMClear:
gDocDirty = true;
TEDelete( textH );
break;
case kEMOptions:
OptionsDialog();
break;
}
SetVertScroll( gDocWindow, gVertScroll );
}else{
/* Handle the operations that we can do while the window is closed */
switch( theItem ){
case kEMOptions:
OptionsDialog();
break;
}
}
}
return true;
}
Boolean DoServMenu( short theItem )
{
OSErr err;
switch( theItem ){
#ifdef NEVER /* This will return in a future version */
case kSMCheckSel:
ToggleSelectCheck();
break;
#endif
case kSMNewBatch:
err = GetNewBatchService();
if ( err )
Gripe( "\pGetNewBatchService failed" );
break;
#ifdef NEVER /* This will return in a future version */
case kSMNewInteractive:
if ( !gDocWindow )
return true;
break;
#endif
case kSMRemoveService:
RemoveServices();
break;
#ifdef NEVER /* This will return in a future version */
case kSMCheckWord:
if ( !gDocWindow )
return true;
break;
#endif
case kSMDash:
break;
default:
/* Here is where we actually do some spellchecking! */
if ( !gDocWindow )
return true;
DoSpellCheck( theItem - kSMDash );
break;
}
return true;
}
Boolean DoMouseUp( void )
{
return true;
}
Boolean DoKeyDown( long message, short modifiers )
{
char theChar;
Boolean result;
theChar = message & charCodeMask;
if ( modifiers & cmdKey ){
FixMenuMarks(); /* We need to have the dis/enabling up to date */
result = DoMenuCommand( MenuKey( theChar ) );
}else {
if ( gDocWindow && ( gDocWindow == FrontWindow() )){
gDocDirty = true;
/* 1.0.3 We need to look for various special cases to
* tell if we need to scroll.
*/
MyTEKey( gDocWindow, gVertScroll, theChar, false );
}
result = true;
}
return result;
}
Boolean DoUpdateEvt( long message )
{
GrafPtr curPort;
BeginUpdate( (WindowPtr)message );
if ( (WindowPtr)message == gDocWindow ){
GetPort( &curPort );
SetPort( gDocWindow );
EraseRect( &( thePort->portRect ) );
TEUpdate( &( thePort->portRect ), (TEHandle)GetWRefCon( (WindowPtr)message ) );
MyDrawGrowIcon( (WindowPtr)message );
DrawControls( gDocWindow );
SetPort( curPort );
}
EndUpdate( (WindowPtr)message );
return true;
}
void MyDrawGrowIcon( WindowPtr theWindow )
{
RgnHandle clipRgn;
Rect newClip;
/* This will fudge the DrawGrowIcon routine so we only draw the outline for the
* vertical scroll bar. It's easier than writing a custom WDEF.
* We have to set a new clip region because the update region will mask off some
* of what we might want to draw.
*
* thePort must be the window that is getting the icon drawn in it.
*/
clipRgn = NewRgn();
if ( !clipRgn )
return;
GetClip( clipRgn );
newClip.top = theWindow->portRect.top;
newClip.right = theWindow->portRect.right;
newClip.bottom = theWindow->portRect.bottom;
newClip.left = newClip.right - 15;
ClipRect( &newClip );
DrawGrowIcon( theWindow );
SetClip( clipRgn );
DisposeRgn( clipRgn ); /* 1.1.1 MDC fix a leak */
return;
}
Boolean DoDiskEvt( void )
{
return true;
}
Boolean DoActivateEvt( short modifiers )
{
if ( gDocWindow ){
if ( modifiers & activeFlag ){
TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
} else {
TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
}
}
return true;
}
void GrowDocWindow( WindowPtr theWindow, Point where )
{
long newSize;
short newWidth;
short newHeight;
GrafPtr curPort;
Rect sizeRect;
SetRect( &sizeRect, 80, 80, 32767, 32767 );
newSize = GrowWindow( theWindow, where, &sizeRect );
if ( newSize == 0L )
return;
newWidth = (short) newSize;
newHeight = (short) ( newSize >> 16 );
GetPort( &curPort );
SetPort( theWindow );
SizeWindow( theWindow, newWidth, newHeight, true );
ResetWindowParts( theWindow );
SetPort( curPort );
return;
}
void ResetWindowParts( WindowPtr theWindow )
{
GrafPtr curPort;
/* Resize all of the components of the document window after the window itself
* has been resized.
*/
GetPort( &curPort );
SetPort( theWindow );
SetTESize( theWindow );
/* We have to redraw the whole window, since the grow icon, scroll bars, and
* text rectange have all changed
*/
InvalRect( &( theWindow->portRect ) );
SizeVertScroll();
SetVertScroll( theWindow, gVertScroll );
SetPort( curPort );
return;
}
Boolean DoOSEvt( EventRecord *eventPtr )
{
Boolean inBackground;
switch ((eventPtr->message >> 24) & 0x0FF) { /* high byte of message */
case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
if ( gDocWindow ){
inBackground = (eventPtr->message & resumeFlag) == 0;
if (inBackground) {
#ifdef NEVER /* Don't use for styled textedit */
ZeroScrap();
TEToScrap();
#endif
if ( gDocWindow ){
TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
}
} else {
#ifdef NEVER /* Don't use for styled textedit */
TEFromScrap();
#endif
if ( gDocWindow ){
TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
}
}
}
default:
break;
}
return true;
}
Boolean DoHighLevelEvent( EventRecord *theEventPtr )
{
OSErr err;
err = AEProcessAppleEvent( theEventPtr );
if ( err ){
return true; /* An error result is OK... */
}
return true;
}
void SetTESize( WindowPtr theWindow )
{
Rect txRect;
TEHandle hTE;
short rectOffset;
GetTERect( &( theWindow->portRect ), &txRect );
hTE = (TEHandle)GetWRefCon( theWindow );
rectOffset = (*hTE)->destRect.top - (*hTE)->viewRect.top;
(*hTE)->viewRect = txRect;
txRect.top += rectOffset;
txRect.bottom += rectOffset;
(*hTE)->destRect = txRect;
TECalText( hTE );
return;
}
void GetTERect( Rect *portRectPtr, Rect *txRectPtr )
{
/* Return the rect for the textedit field that will fit in the
* given portRect, allowing for the scroll bar, and a little bit
* of margin around all the edges.
*/
*txRectPtr = *portRectPtr;
InsetRect( txRectPtr, kTextInset, kTextInset );
txRectPtr->right -= 16;
return;
}
void DoQuit( void )
{
if ( gDocWindow )
DoCloseWindow();
ExitToShell();
return;
}
void DoZoom( WindowPtr theWindow, short deskPart )
{
Boolean isFront;
if ( FrontWindow() == theWindow )
isFront = true;
else
isFront = false;
ZoomWindow( theWindow, deskPart, isFront );
ResetWindowParts( theWindow );
return;
}